<?php
namespace Admin\Controller;
use Think\Controller;
class SecuritycController extends CommonController {
    
    public function listc(){
        $listinfo = M('security')->select();
        //print_r($listinfo);
        $this->assign('listinfo',$listinfo);
        $this->display();
    }

    public function goods_list(){
    	$get = I('get.');
    	$po = I('post.');
    	//print_r($get['supplier_id']);
    	$goodslist = M('goods')->where(array('supplier_id'=>$get['supplier_id']))->select();
    	//
    	foreach ($goodslist as $key => $value) {
    		$suinfo = M('security')->where(array('supplier_id'=>$value['supplier_id']))->find();
    		$goodslist[$key]['supplier_name'] = $suinfo['supplier_name'];

    		$coer = M('security_qr')->where(array('goods_id'=>$value['goods_id']))->count();
    		$goodslist[$key]['er_num'] = $coer;
    	}
    	//print_r($goodslist);
    	$this->assign('goodslist',$goodslist);
    	$this->display();
    }

    //生成二维码序列号
    public function er_send(){
    	$get = I('get.');
    	$po = I('post.');
    	//自定义传值

    	$info['number'] = '0';
    	$info['send_sum'] = '100';
    	$info['goods_id'] = $get['goods_id'];
    	$info['supplier_id'] = $get['supplier_id'];

    	$this->assign('info',$info);
    	$this->display('security_send');
    }

    //发放二维码
    public function send_by_print(){
    	$get = I('get.');
    	$po = I('post.');
    	//print_r($po);

    	$add_time=time();
	    $goods_id = !empty($po['goods_id']) ? $po['goods_id'] : 0;
	    $supplier_id = !empty($po['supplier_id']) ? $po['supplier_id'] : 0;
	    $send_sum    = !empty($po['send_sum'])     ? $po['send_sum']     : 1;
	    $number = !empty($po['number']) ? $po['number'] : 0;

	    $j=0;

	    while ($j < $send_sum)
	    {
	        $returnStr = $this->qr_sn();

	        
	        $qr_sn = M('security_qr')->where(array('qr_sn'=>$returnStr))->getField('qr_sn');

	        if (!$qr_sn)
	        {
	        	
	        	$data = array(
	        		'supplier_id'=>$supplier_id,
	        		'goods_id'=>$goods_id,
	        		'qr_sn'=>$returnStr,
	        		'number'=>$number,
	        		'add_time'=>$add_time
	        		);
	        	//$result = M('security_qr')->data($data)->add();

	            $j++;
	        }	        
	    }
	    //$this->success('生成二维码序列号成功!', 'User/list');
	    $this->success('生成二维码序列号成功',U('Securityc/goods_list',array('supplier_id'=>$supplier_id)));
	   // $this->redirect('Securityc/goods_list', array('supplier_id' => $supplier_id), 5, '页面跳转中...');
    }
    //生成随机字符 纯数字
    public function qr_sn(){
	    $returnStr = substr(strrev(microtime()), 0, 3) .substr(strtotime(date("Y-m-d", time())), 0, 3) . substr(strrev(microtime()), 0, 3) . substr(mt_rand(), 0, 4) . substr(rand(), 0, 3);
	    return $returnStr;
	}

}